/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.service.impl;

import com.minierp.dao.AccessoryDao;
import com.minierp.dao.AdjustBillDao;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.CooperBillDao;
import com.minierp.dao.EstPriceDao;
import com.minierp.dao.EstablishmentCommunicateDao;
import com.minierp.dao.EstablishmentContactDao;
import com.minierp.dao.EstablishmentDao;
import com.minierp.dao.FinanceDetailDao;
import com.minierp.dao.InBillDao;
import com.minierp.dao.OutBillDao;
import com.minierp.dao.PayBillDao;
import com.minierp.dao.PurchaseBillDao;
import com.minierp.dao.ReceiveBillDao;
import com.minierp.err.NotAllowException;
import com.minierp.model.Establishment;
import com.minierp.service.EstablishmentService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class EstablishmentServiceImpl implements EstablishmentService {
	public void txClientCorporate(long[] ids,
			EstablishmentDao establishmentDao, BusinessBillDao businessBillDao,
			AdjustBillDao adjustBillDao, ReceiveBillDao receiveBillDao,
			InBillDao inBillDao, OutBillDao outBillDao,
			EstablishmentContactDao establishmentContactDao,
			EstablishmentCommunicateDao establishmentCommunicateDao,
			FinanceDetailDao financeDetailDao, AccessoryDao accessoryDao,
			EstPriceDao estPriceDao) throws Exception {
		if (ids.length > 1) {
			ArrayList estList = new ArrayList();
			Establishment targetEst = null;
			BigDecimal busCost = new BigDecimal(0);
			String mark = "";

			int i;
			Establishment est;
			for (i = 0; i < ids.length; ++i) {
				est = (Establishment) establishmentDao.get(Establishment.class,
						ids[i]);
				if (est == null) {
					throw new NotAllowException("Client not found for id:"
							+ ids[i]);
				}

				estList.add(est);
				if (est.getMark() != null && !est.getMark().equals("")) {
					if (!mark.equals("")) {
						mark = mark + " ";
					}

					mark = mark + est.getMark();
				}

				busCost = busCost.add(est.getBusCost());
				if (targetEst == null) {
					targetEst = est;
				} else if (targetEst.getBusTime() == null) {
					if (est.getBusTime() != null) {
						targetEst = est;
					}
				} else if (est.getBusTime() != null
						&& targetEst.getBusTime().before(est.getBusTime())) {
					targetEst = est;
				}
			}

			targetEst.setBusCost(busCost);
			targetEst.setMark(mark);
			establishmentDao.update(targetEst);

			for (i = 0; i < estList.size(); ++i) {
				est = (Establishment) estList.get(i);
				if (est.getId() != targetEst.getId()) {
					businessBillDao
							.estCorporate(est.getId(), targetEst.getId());
					adjustBillDao.estCorporate(est.getId(), targetEst.getId());
					receiveBillDao.estCorporate(est.getId(), targetEst.getId());
					inBillDao.estCorporate(est.getId(), targetEst.getId(),
							"CLIENT");
					outBillDao.estCorporate(est.getId(), targetEst.getId(),
							"CLIENT");
					establishmentContactDao.estCorporate(est.getId(),
							targetEst.getId());
					establishmentCommunicateDao.estCorporate(est.getId(),
							targetEst.getId());
					accessoryDao.estCorporate(est.getId(), targetEst.getId());
					financeDetailDao.estCorporate(est.getId(),
							targetEst.getId());
					estPriceDao.estCorporate(est.getId(), targetEst.getId());
					establishmentDao.logCorporate(est.getId(),
							targetEst.getId());
					List childList = establishmentDao.getAllChildren(est
							.getId());

					for (int j = 0; j < childList.size(); ++j) {
						Establishment child = (Establishment) childList.get(j);
						String superiorIdStr = child.getSuperiorIdStr();
						superiorIdStr.replace(";" + est.getId() + ";", ";");
						child.setSuperiorIdStr(superiorIdStr);
						if (child.getSuperiorId() == est.getId()) {
							child.setSuperiorId(0L);
						}

						establishmentDao.update(child);
					}

					establishmentDao.delete(est.getId());
				}
			}
		}

	}

	public void txCooperatorCorporate(long[] ids,
			EstablishmentDao establishmentDao, CooperBillDao cooperBillDao,
			AdjustBillDao adjustBillDao, PayBillDao payBillDao,
			InBillDao inBillDao, OutBillDao outBillDao,
			EstablishmentContactDao establishmentContactDao,
			EstablishmentCommunicateDao establishmentCommunicateDao,
			FinanceDetailDao financeDetailDao, AccessoryDao accessoryDao,
			EstPriceDao estPriceDao) throws Exception {
		if (ids.length > 1) {
			ArrayList estList = new ArrayList();
			Establishment targetEst = null;
			String mark = "";

			int i;
			Establishment est;
			for (i = 0; i < ids.length; ++i) {
				est = (Establishment) establishmentDao.get(Establishment.class,
						ids[i]);
				if (est == null) {
					throw new NotAllowException("Client not found for id:"
							+ ids[i]);
				}

				estList.add(est);
				if (est.getMark() != null && !est.getMark().equals("")) {
					if (!mark.equals("")) {
						mark = mark + " ";
					}

					mark = mark + est.getMark();
				}

				if (targetEst == null) {
					targetEst = est;
				} else if (targetEst.getBusTime() == null) {
					if (est.getBusTime() != null) {
						targetEst = est;
					}
				} else if (est.getBusTime() != null
						&& targetEst.getBusTime().before(est.getBusTime())) {
					targetEst = est;
				}
			}

			targetEst.setMark(mark);
			establishmentDao.update(targetEst);

			for (i = 0; i < estList.size(); ++i) {
				est = (Establishment) estList.get(i);
				if (est.getId() != targetEst.getId()) {
					cooperBillDao.estCorporate(est.getId(), targetEst.getId());
					adjustBillDao.estCorporate(est.getId(), targetEst.getId());
					payBillDao.estCorporate(est.getId(), targetEst.getId());
					inBillDao.estCorporate(est.getId(), targetEst.getId(),
							"COOPERATOR");
					outBillDao.estCorporate(est.getId(), targetEst.getId(),
							"COOPERATOR");
					establishmentContactDao.estCorporate(est.getId(),
							targetEst.getId());
					establishmentCommunicateDao.estCorporate(est.getId(),
							targetEst.getId());
					accessoryDao.estCorporate(est.getId(), targetEst.getId());
					financeDetailDao.estCorporate(est.getId(),
							targetEst.getId());
					estPriceDao.estCorporate(est.getId(), targetEst.getId());
					List childList = establishmentDao.getAllChildren(est
							.getId());

					for (int j = 0; j < childList.size(); ++j) {
						Establishment child = (Establishment) childList.get(j);
						String superiorIdStr = child.getSuperiorIdStr();
						superiorIdStr.replace(";" + est.getId() + ";", ";");
						child.setSuperiorIdStr(superiorIdStr);
						if (child.getSuperiorId() == est.getId()) {
							child.setSuperiorId(0L);
						}

						establishmentDao.update(child);
					}

					establishmentDao.delete(est.getId());
				}
			}
		}

	}

	public void txProviderCorporate(long[] ids,
			EstablishmentDao establishmentDao, PurchaseBillDao purchaseBillDao,
			AdjustBillDao adjustBillDao, PayBillDao payBillDao,
			InBillDao inBillDao, OutBillDao outBillDao,
			EstablishmentContactDao establishmentContactDao,
			EstablishmentCommunicateDao establishmentCommunicateDao,
			FinanceDetailDao financeDetailDao, AccessoryDao accessoryDao,
			EstPriceDao estPriceDao) throws Exception {
		if (ids.length > 1) {
			ArrayList estList = new ArrayList();
			Establishment targetEst = null;
			String mark = "";

			int i;
			Establishment est;
			for (i = 0; i < ids.length; ++i) {
				est = (Establishment) establishmentDao.get(Establishment.class,
						ids[i]);
				if (est == null) {
					throw new NotAllowException("Client not found for id:"
							+ ids[i]);
				}

				estList.add(est);
				if (est.getMark() != null && !est.getMark().equals("")) {
					if (!mark.equals("")) {
						mark = mark + " ";
					}

					mark = mark + est.getMark();
				}

				if (targetEst == null) {
					targetEst = est;
				} else if (targetEst.getBusTime() == null) {
					if (est.getBusTime() != null) {
						targetEst = est;
					}
				} else if (est.getBusTime() != null
						&& targetEst.getBusTime().before(est.getBusTime())) {
					targetEst = est;
				}
			}

			targetEst.setMark(mark);
			establishmentDao.update(targetEst);

			for (i = 0; i < estList.size(); ++i) {
				est = (Establishment) estList.get(i);
				if (est.getId() != targetEst.getId()) {
					purchaseBillDao
							.estCorporate(est.getId(), targetEst.getId());
					adjustBillDao.estCorporate(est.getId(), targetEst.getId());
					payBillDao.estCorporate(est.getId(), targetEst.getId());
					inBillDao.estCorporate(est.getId(), targetEst.getId(),
							"PROVIDER");
					outBillDao.estCorporate(est.getId(), targetEst.getId(),
							"PROVIDER");
					establishmentContactDao.estCorporate(est.getId(),
							targetEst.getId());
					establishmentCommunicateDao.estCorporate(est.getId(),
							targetEst.getId());
					accessoryDao.estCorporate(est.getId(), targetEst.getId());
					financeDetailDao.estCorporate(est.getId(),
							targetEst.getId());
					estPriceDao.estCorporate(est.getId(), targetEst.getId());
					List childList = establishmentDao.getAllChildren(est
							.getId());

					for (int j = 0; j < childList.size(); ++j) {
						Establishment child = (Establishment) childList.get(j);
						String superiorIdStr = child.getSuperiorIdStr();
						superiorIdStr.replace(";" + est.getId() + ";", ";");
						child.setSuperiorIdStr(superiorIdStr);
						if (child.getSuperiorId() == est.getId()) {
							child.setSuperiorId(0L);
						}

						establishmentDao.update(child);
					}

					establishmentDao.delete(est.getId());
				}
			}
		}

	}
}